package com.example.deliveryserver.service;

import com.example.deliveryserver.dao.DeliveryAddressDao;
import com.example.deliveryserver.entities.DeliveryAddress;
import com.example.server_engineer.service.AbstractTypedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author Administrator
 */
@Slf4j
@Service
public class DeliveryAddressService
        extends AbstractTypedService<DeliveryAddress, Integer> {

    DeliveryAddressDao deliveryAddressDao;

    DeliveryAddressService(DeliveryAddressDao dao) {
        this.dataContext = dao;
        this.deliveryAddressDao = dao;
    }

    public List<DeliveryAddress> queryAll() {
        List<DeliveryAddress> lst = null;
        try {
            lst = ((DeliveryAddressDao) this.dataContext).findAll();
            log.info("find orders: " + lst.size());
        } catch (Exception e) {
            log.warn(e.toString());
        }

        return lst;
    }

    public Page<DeliveryAddress> queryOrderByPage(Pageable pageable) {
        return deliveryAddressDao.queryPage(pageable, null);
    }

    // 根据用户 id查询地址信息
    public DeliveryAddress queryAddress(String userid){
        return deliveryAddressDao.findDeliveryAddressByUserId(userid);
    }

    public Optional<DeliveryAddress> queryAddressById(Integer id){
        return deliveryAddressDao.findDeliveryAddressById(id);
    }
}
